<?

class FileUpload extends Controller{
	var $upload_config = array(
							'upload_path' 	=> './uploads/',
							'allowed_types' => 'jpg|png|gif|pdf|doc|odf|xls'
						);
	
	var $form_view = null;
	var $error_view = null;
	var $success_view = null;
	
	var $params = array('controller' => 'fileupload');
	var $model = null;
	var $file_location_field = 'image_path';
	
	function FileUpload(){
		parent::Controller();
		$this->load->helper('file');
		$this->form_view = CMF_PREFIX . 'upload_form';
		$this->error_view = CMF_PREFIX . 'upload_failure';
		$this->success_view = CMF_PREFIX . 'upload_success';
	}
	
	function index(){
		if( !empty($this->form_view) ){
			$this->load->view($this->form_view, $this->params);
		}
	}
	
	function doUpload( $fieldname = 'userfile', $timestamp = 1){
		create_path($this->upload_config['upload_path']);
		$this->load->library('upload', $this->upload_config);
		if( !$this->upload->do_upload( $fieldname, $timestamp ) ){
			if( !empty($this->error_view)){
				$error = array('error' => $this->upload->display_errors());
				$this->load->view($this->error_view, $error);
			}else{
				show_error('FileUpload::doUpload - ' . $this->upload->display_errors());
			}
		}else{
			$data = array( 'upload_data' => $this->upload->data());
			
			$this->updateDatabase($data['upload_data']);
			if( !empty($this->success_view) ){
				$this->load->view($this->success_view, $data);
			}else{
				show_error('FileUpload::doUpload - no defined success template');
			}
		}
	}
	
	function updateDatabase(&$upload_data){
		if( $this->model ){
			/* TODO: need to create an image upload class to set the proper model and the file location field, then we can just use that instead of having multiple classes */
			//$test = $this->model->getFileLocationField();
			$test = $this->file_location_field;
			$doc_root = realpath($_SERVER['DOCUMENT_ROOT']);
			$uri = str_replace($doc_root, '', $upload_data['full_path']);
			//$upload_data[$this->model->getFileLocationField()] = $uri;
			$upload_data[$this->file_location_field] = $uri;
			$upload_data['response_code'] = 2;
			
			$image_id = $this->model->saveItem($upload_data);
			$upload_data['uri'] = $uri;
			$upload_data['image_id'] = $image_id;
			
			/* fine for now, but need to have seperate for uploaded files and uploaded images */
			$uploaded_images = $this->session->userdata('uploaded_images');
			if( $uploaded_images ){
				$uploaded_images = unserialize($uploaded_images);
			}
			if( !is_array($uploaded_images)){
				$uploaded_images = array();
			}
			$uploaded_images[] = $image_id;
			$this->session->set_userdata('uploaded_images', serialize($uploaded_images));
			
			/* TODO: will have to change this to allow for custom ids */
			/* am going to remove this and have the parent model deal with adding relationships */
			/*
			$id = $this->input->post('project_id');
			if( !empty($id) && !empty($image_id) && is_numeric($id)){
				$upload_data['response_code'] = 1;
				$this->model->addRelatedItems( $id, $image_id);
			}*/
		}
	}
}
